; 
; Revision date:

INCLUDE Irvine32.inc 
;
;	Parse PDP8 Instructions 
;	Author: Phil Hartley
;

OperandShift		EQU	9
IndirectBitMask	EQU	400O
Page0BitMask		EQU	200O
AddressMask		EQU	177O
.data
PDP8CODE	WORD 7300O	; CLA CLR
		WORD	3050O	; DCA 50o
		WORD 3051O	; DCA 51o
		WORD 1063O	; TAD 63o
		WORD 7004O	; RAL
		WORD 4245O	; JMS 45o
NumberOfInstructions	DWORD 6
InstructionLocation		DWORD 0
Mnemonics	BYTE	"AND", 0, "TAD", 0, "ISZ", 0, "DCA", 0, "JMS", 0, "JMP", 0, "IOT", 0, "OPR", 0
IndirectBitOn	BYTE " Indirect bit on ", 0
IndirectBitOff	BYTE	" Indirect bit off ", 0
Page0BitOn	BYTE " Page 0 bit on ", 0
Page0BitOff	BYTE " Page 0 bit off ", 0
AddressString	BYTE	" Address (in octal): "
.code
main		PROC
		mov ECX, NumberOfInstructions
		mov EBX, OFFSET PDP8CODE
		mov ESI, InstructionLocation
next:	mov EAX, 0
		mov AX, [EBX + ESI]		; Load the instruction into AX
		call printMnemonic
		call printIndirectBit
		call printPage0Bit
		call printAddress
		call CrLf
		add ESI, 2			; Next instruction, i.e. add two bytes
		loop next
		call WaitMsg
		exit
main		ENDP

printMnemonic	PROC USES EAX EDX
		shr AX, OperandShift
		mov EDX, OFFSET Mnemonics
		shl AX, 2				; Multiply by 4
		add EDX, EAX
		call WriteString
		ret
printMnemonic	ENDP
		
printIndirectBit PROC USES EAX EDX
		mov EDX, OFFSET IndirectBitOn
		test EAX, IndirectBitMask
		jnz BitOn1
		mov EDX, OFFSET IndirectBitOff
BitOn1:	call WriteString
		ret
printIndirectBit ENDP
	
printPage0Bit	PROC USES EAX EDX
		mov EDX, OFFSET Page0BitOn
		test EAX, Page0BitMask
		jnz BitOn2
		mov EDX, OFFSET Page0BitOff
BitOn2:	call WriteString
		ret
printPage0Bit	ENDP

printAddress	PROC USES EAX EDX
		mov EDX, OFFSET AddressString
		call WriteString
		and EAX, 	AddressMask	; An address in a PDP-8 instruction is 7 bits: 3 octal digits
		call printOctal
		ret
printAddress	ENDP

;
; Print an octal address supplied in AX
;
printOctal	PROC USES EAX EDX
		; Save contents of EAX in EDX
		mov edx, eax
		and ax, 7000O		; Top nibble
		shr ax, 9
		call WriteDec
		mov eax, edx
		and ax, 700O
		shr ax, 6
		call WriteDec
		mov eax, edx
		and ax, 70O
		shr ax, 3
		call WriteDec
		mov eax, edx 
		and ax, 7O
		call WriteDec
		ret
printOctal	ENDP

END main